#!/usr/bin/python
# encoding:utf-8

# ****************************************************************
# Description： congfig相关配置
# Author： jf
# Date：20170605
# version：
# ****************************************************************

import ConfigParser
import os
import re


# 获取config配置文件
# def getConfig(section, key):
#     config = ConfigParser.ConfigParser()
# 	# 得到的是当前文件模块的目录
#     #path = os.path.split(os.path.realpath(__file__))[0] + '/config.ini'
#     config.read('config.ini')
#     return config.get(section, key)

def pfile(configpath):
	# Windows文件预处理
	content = open(configpath).read()
	content = re.sub(r"\xfe\xff", "", content)
	content = re.sub(r"\xff\xfe", "", content)
	content = re.sub(r"\xef\xbb\xbf", "", content)
	open(configpath, 'w').write(content)


def getConfig(section, key):
	currentpath = os.getcwd()
	configpath = currentpath + '\\Config\\config.ini'

	# 文件预处理
	pfile(configpath)

	if not os.path.exists(configpath):
		print "can't find config file...\r\n"
		return
	else:
		config = ConfigParser.ConfigParser()
		config.read(configpath)
	return config.get(section, key)


def setConfig(section, key, value):
	currentpath = os.getcwd()
	configpath = currentpath + '\\Config\\config.ini'

	# 文件预处理
	pfile(configpath)

	if not os.path.exists(configpath):
		print "can't find config file...\r\n"
		return
	else:
		config = ConfigParser.ConfigParser()
		config.read(configpath)

	config.set(section, key, value)
	config.write(open(configpath, "w"))


def setConfigSheetname(section, key, value):
	value = value.upper()
	currentpath = os.getcwd()
	configpath = currentpath + '\\Config\\config.ini'

	# 文件预处理
	pfile(configpath)

	if not os.path.exists(configpath):
		print "can't find config file...\r\n"
	else:
		config = ConfigParser.ConfigParser()
		config.read(configpath)

		# 先读取原来的值
		sheetnameget = getConfig("CFG", "SheetName").upper()
		# 如果写入的值已有，直接返回
		if sheetnameget.find(value) != -1:
			return
		elif value == "ALL":
			value = 'basic,phonebook,message,network,call,safe,TCPIP,wifi'
			config.set(section, key, value)
			config.write(open(configpath, "w"))
		elif value == "NONE":
			config.set(section, key, value)
			config.write(open(configpath, "w"))
		else:
			if sheetnameget != "ALL," and sheetnameget != "NONE":
				value = sheetnameget + ',' + value

			config.set(section, key, value)
			config.write(open(configpath, "w"))


def setConfigSheetnameDel(section, key, value):
	value = value.upper()
	currentpath = os.getcwd()
	configpath = currentpath + '\\Config\\config.ini'

	# 文件预处理
	pfile(configpath)

	if not os.path.exists(configpath):
		print "can't find config file...\r\n"
		return
	else:
		config = ConfigParser.ConfigParser()
		config.read(configpath)

	# 先读取原来的值
	sheetnameget = list((getConfig("CFG", "SheetName").upper()).split(","))
	# print sheetnameget, 'sheetnameget'
	# print value,type(value),"value"
	sheetnameget.remove(value)
	# print sheetnameget, 'sheetnameget'
	if "ALL" in sheetnameget:
		sheetnameget.remove('ALL')
	if not sheetnameget:
		sheetnameget = "NONE"
	else:
		sheetnameget = str(sheetnameget).replace('[', '').replace(']', '').replace('\'', '').replace(' ', '')
	# print sheetnameget, 'rem later'
	config.set(section, key, sheetnameget)
	config.write(open(configpath, "w"))


if __name__ == "__main__":
	sheetname = getConfig("CFG", "SheetName")
	Mail_List = getConfig("CFG", "Mail_List")
	Path_Excel = getConfig("CFG", "Path_Excel")
	print sheetname
	print Mail_List
	print Path_Excel
